layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;


    //测试连接
    form.on('submit(testConnection)', function (data) {
        console.log(data.field)
        /*  postAjax(basePath + "/database/connectTest", data.field, function (result) {
              if(result && result.code === 0){
                  var index = layer.alert(result.msg, {icon: 1, closeBtn: 0 }, function () {
                      layer.close(index);
                  });
              }else if(result && result.msg){
                  layer.alert(result.msg, {icon: 2});
              }else {
                  layer.alert("连接失败", {icon: 2});
              }
          });*/
        $.ajax({
            type: "POST",
            url: "/database/connectTest",
            data: JSON.stringify(data.field),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function(result) {
                if(result && result.code === 0){
                    var index = layer.alert(result.msg, {icon: 1, closeBtn: 0 }, function () {
                        layer.close(index);
                    });
                }else if(result && result.msg){
                    layer.alert(result.msg, {icon: 2});
                }else {
                    layer.alert("连接失败", {icon: 2});
                }
            }
        });
        return false;
    });


    //保存
    form.on('submit(saveDatabase)', function (data) {
       /* postAjax( "http://localhost:8080/database/add", data.field, function (result) {
            if(result && result.code === 0){
                var index = layer.alert(result.msg, { title: '提示', closeBtn: 0 }, function () {
                    layer.close(index);
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    //window.parent.reload()
                    parent.layer.close(iframeIndex);
                    location.reload()
                });
            }else if(result && result.msg){
                layer.alert(result.msg, { title: '提示' });
            }else {
                layer.alert("保存失败", { title: '提示' });
            }
        });*/
        $.ajax({
            type: "POST",
            url: "/database/add",
            data: JSON.stringify(data.field),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function(result) {
                if(result && result.code === 0){
                    var index = layer.alert(result.msg, { title: '提示', closeBtn: 0 }, function () {
                        layer.close(index);
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        //window.parent.reload()
                        parent.layer.close(iframeIndex);
                        location.reload()
                    });
                }else if(result && result.msg){
                    layer.alert(result.msg, { title: '提示' });
                }else {
                    layer.alert("保存失败", { title: '提示' });
                }
            }
        });
        return false;
    });
});